package yl.hs.bmipfull.service.material.impl;


import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratory.IPackageInfoDao;
import yl.hs.bmipfull.daos.material.IRawMaterialInventoryDao;
import yl.hs.bmipfull.daos.material.IRawMaterialTakeInventoryDao;
import yl.hs.bmipfull.daos.material.IWarehouseDao;
import yl.hs.bmipfull.daos.production.IFinishProductInventoryDao;
import yl.hs.bmipfull.pojo.laboratory.PackageInfoEntity;
import yl.hs.bmipfull.pojo.material.RawMaterialTakeInventoryEntity;
import yl.hs.bmipfull.pojo.production.FinishProductInventoryEntity;
import yl.hs.bmipfull.service.material.IRawMaterialTakeInventoryService;
import yl.hs.bmipfull.service.production.IFinishProductInventoryService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author 张凯
 * @apiNote 仓库盘存服务层接口实现
 * @since 2022年05月09日 15时00分30秒 <br/>
 */
@Service
public class RawMaterialTakeInventoryServiceImpl extends PurchaseBaseService implements IRawMaterialTakeInventoryService {

    @Autowired
    IRawMaterialInventoryDao rawMaterialInventoryDao;
    @Autowired
    IRawMaterialTakeInventoryDao dao;
    @Autowired
    IWarehouseDao warehouseDao;
    @Autowired
    IFinishProductInventoryDao finishProductInventoryDao;
    @Autowired
    IPackageInfoDao packageInfoDao;

    @Override
    public List<RawMaterialTakeInventoryEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(RawMaterialTakeInventoryEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<RawMaterialTakeInventoryEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(RawMaterialTakeInventoryEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<RawMaterialTakeInventoryEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public RawMaterialTakeInventoryEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveRawMaterialTakeInventory(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        RawMaterialTakeInventoryEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new RawMaterialTakeInventoryEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(RawMaterialTakeInventoryEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                if (entity.getState() == 2) {
                    var wareHouse = rawMaterialInventoryDao.findByWHSNAndRmSN(entity.getInventorySN(), entity.getRmSN());
                    if (wareHouse != null) {
                        wareHouse.setQuantity(entity.getInventoryQuantity());
                        rawMaterialInventoryDao.update(wareHouse);
                        var cangKus = warehouseDao.findBySN(wareHouse.getWhSN());
                        cangKus.setInventory(cangKus.getInventory().add(entity.getInventoryQuantity().subtract(entity.getBookQuantity())));
                        warehouseDao.update(cangKus);
                    }
                    writeLog(ActionType.CREATE, entity.getSn(), "插入仓库盘存信息", Tl.toJson(entity), RawMaterialTakeInventoryEntity.class);
                }
            } else {
                //执行修改
                entity = findBySn(sn);
                Tl.getChangeLog(entity, model);
                entity.setPYFieldValue();
                result = dao.update(entity);
                if (result > 0) {
                    if (entity.getState() == 2) {
                        var wareHouse = rawMaterialInventoryDao.findByWHSNAndRmSN(entity.getInventorySN(), entity.getRmSN());
                        if (wareHouse != null) {
                            wareHouse.setQuantity(entity.getInventoryQuantity());
                            rawMaterialInventoryDao.update(wareHouse);
                            var cangKu = warehouseDao.findBySN(wareHouse.getWhSN());
                            cangKu.setInventory(entity.getInventoryQuantity());
                            warehouseDao.update(cangKu);
                        }
                    }
                    writeLog(ActionType.UPDATE, sn, "更新仓库盘存信息", Tl.toJson(entity), RawMaterialTakeInventoryEntity.class);
                }
            }
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, RawMaterialTakeInventoryEntity.class);
        }
        return result;
    }
}